<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyWord" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60" label="序号">
      </el-table-column>
      <el-table-column prop="name" label="店铺名" width="140" >
      </el-table-column>
      <el-table-column prop="tel" label="电话" width="100" >
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" width="100" >
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="100" >
        <template slot-scope="scope">
          <span v-if="scope.row.state==1" style="color: cadetblue">待审核</span>
          <span v-if="scope.row.state==2" style="color: yellowgreen">审核通过待激活</span>
          <span v-if="scope.row.state==3" style="color: green">激活成功</span>
          <span v-if="scope.row.state==4" style="color: red">驳回</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="地址" min-width="100" >
      </el-table-column>
      <el-table-column prop="logo" label="商标" min-width="100" >
        <template slot-scope="scope">
          <img :src="scope.row.logo" width="40px" height="40px"/>
        </template>
      </el-table-column>
      <el-table-column prop="admin.username" label="店铺管理员" min-width="80" >
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="success" size="small" v-if="scope.row.state==1 || scope.row.state==4" @click="handleCheck(scope.$index, scope.row)">人工审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" :current-page="query.currentPage" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增/修改界面-->
    <el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm" >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="saveForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="saveForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门">
          <!--          <el-input v-model="saveForm.parent.id" auto-complete="off"></el-input>-->
          <el-cascader
              v-model="saveForm.department"
              :options="deptTree"
              :props="{
							   checkStrictly: true,
							   label:'name',
							   value:'id'
							}"
              clearable
          >
          </el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="saveLoading" >提交</el-button>
      </div>
    </el-dialog>


    <!--人工审核界面-->
    <el-dialog title="人工审核" :visible.sync="saveCheckVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" >
        <el-form-item label="店铺名">
          <el-input v-model="saveForm.shop.name" auto-complete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="店铺地址">
          <el-input v-model="saveForm.shop.address" auto-complete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="驳回内容(驳回必填)">
          <el-input v-model="saveForm.content" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click.native="saveReject" :loading="saveRejectLoading">驳回</el-button>
        <el-button type="success" @click.native="saveAccess" :loading="saveAccessLoading" >通过</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

export default {
  data() {
    return {
      saveCheckVisible:false,
      listLoading: false,
      sels: [],//列表选中列
      query:{
        currentPage:1,
        pageSize:10,
        keyWord:''
      },
      pageInfo:{
        total:0,
        rows:[]
      },
      saveFormVisible: false,//编辑界面是否显示
      saveAccessLoading: false,
      saveRejectLoading: false,
      saveFormRules: {
        username: [
          { required: true, message: '请输入名字', trigger: 'blur' }
        ]
      },
      //新增界面数据
      saveForm: {
        shop:{
          id:null,
          name:'',
          address:''
        },
        content:''
      },
      roles:[],
      deptTree:[],
      saveRoleVisible:false,
      saveRoleLoading:false,
      saveEmpRole:{
        shop_id:null,
        role_id:null
      }
    }
  },
  methods: {
    //审核驳回 直接发失败邮件
    saveReject(){
      this.saveRejectLoading = true;
      this.$http.post("/shop/rejectAndAccess",this.saveForm)
          .then(result => {
            console.log(result)
            result = result.data;
            if (result.success){
              this.saveRejectLoading = false;
              this.saveCheckVisible = false;
              this.getShopList();
              this.$message({message:result.message,type:"success"});
            }else{
              this.saveRejectLoading = false;
              this.saveCheckVisible = false;
              this.getShopList();
              this.$message({message:result.message,type:"error"});
            }
          })
          .catch(result =>{
            this.$message({message:"网络异常,请检查网络!",type:"error"});
            this.saveRejectLoading = false;
            this.saveCheckVisible = false;
          })
    },
    //审核通过 直接发激活邮件
    saveAccess(){
      this.saveAccessLoading = true;
      this.$http.post("/shop/rejectAndAccess",this.saveForm)
          .then(result => {
            console.log(result)
            result = result.data;
            if (result.success){
              this.saveAccessLoading = false;
              this.saveCheckVisible = false;
              this.getShopList();
              this.$message({message:result.message,type:"success"});
            }else{
              this.saveAccessLoading = false;
              this.saveCheckVisible = false;
              this.getShopList();
              this.$message({message:result.message,type:"error"});
            }
          })
          .catch(result =>{
            this.$message({message:"网络异常,请检查网络!",type:"error"});
            this.saveAccessLoading = false;
            this.saveCheckVisible = false;
          })
    },
    handleCheck(index,row){
      this.saveCheckVisible=true;
      this.saveForm.shop=row;
    },
    //获取角色列表
    getRoles() {
      this.$http.get("/role")
          .then(result => {
            result = result.data;
            if(result.success){
              this.roles=result.data;
            }else {
              this.$message({message:result.message,type:'error'});
            }
          })
          .catch(result => {
            this.$message({message:'网络异常!',type:'error'});
          })
    },
    //保存角色
    saveRole(){
      this.$http.put("/role/empRole",this.saveEmpRole)
          .then(result => {
            result = result.data;
            if(result.success){
              this.saveRoleVisible=false;
              this.$message({message:'设置角色成功',type:'success'});
            }else {
              this.$message({message:result.message,type:'error'});
            }
          })
          .catch(result => {
            this.$message({message:'网络异常!',type:'error'});
          })
    },
    //设置角色
    handleRole(index,row){
        this.getEmpRole(row.id);
        this.saveEmpRole.shop_id=row.id;
        this.saveRoleVisible=true;
        this.getRoles();
    },
    getEmpRole(id){
      this.$http.get("/role/getEmpRole/"+id)
          .then(result => {
            result = result.data;
            if(result.success){
              this.saveEmpRole.role_id=result.data.role_id
            }else {
              this.saveEmpRole.role_id=null;
            }
          })
          .catch(result => {
            this.$message({message:'网络异常!',type:'error'});
          })
    },
    handleCurrentChange(val) {
      console.log(val)
      this.query.currentPage = val;
      this.getShopList();
    },
    search(){
      this.query.currentPage=1;
      this.getShopList();
    },
    //获取员工列表
    getShopList() {
      this.listLoading = true;
      console.log(this.query)
      this.$http.post("/shop",this.query)
          .then(result => {
            console.log(result)
            result = result.data;
            if (result.success){
              this.pageInfo=result.data;
              this.listLoading = false;
            }else{
              this.$message({message:result.message,type:"error"});
            }
          })
          .catch(result =>{
            this.$message({message:"网络异常,请检查网络!",type:"error"});
          })
    },
    //获取子部门树
    getDeptChildTree(){
      this.$http.get("/department/ctree")
          .then(result => {
            console.log(result)
            result = result.data;
            if (result.success){
              this.deptTree=result.data;
              console.log(this.deptTree);
            }else{
              this.$message({message:result.message,type:"error"});
            }
          })
          .catch(result =>{
            this.$message({message:"网络异常,请检查网络!",type:"error"});
          })
    },
    //单条删除
    handleDel: function (index, row) {
      console.log(row)
      this.$confirm('确认删除该数据吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.delete("/shop/"+row.id)
            .then(result => {
              console.log(result);
              result = result.data;
              if (result.success){
                this.$message({message:"删除成功!",type:"success"});
                this.query.currentPage=1;
                this.getShopList();
              }else{
                this.$message({message:result.message,type:"error"});
              }
            })
            .catch(result =>{
              this.$message({message:"网络异常,请检查网络!",type:"error"});
            })
      }).catch(() => {
        this.$message({message:"已取消删除!",type:"info"});
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.saveFormVisible = true;
      console.log("-------------------");
      console.log(row);
      console.log("-------------------");
      this.saveForm = Object.assign({}, row);
      if(!this.saveForm.department){
        this.saveForm.department={
          id:null,
          name:'',
          path:''
        }
      }else {
        let array = this.saveForm.department.path.split("/");
        console.log("array=",array)
        let temp = [];
        for (let i=1;i<array.length;i++){
          temp.push(parseInt(array[i]));
        }
        this.saveForm.department=temp;
        console.log(temp);
      }
      this.getDeptChildTree();
    },
    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.saveForm = {
        id:null,
        username:'',
        password:'',
        email:'',
        age:'',
        department:{
          id:null,
          name:'',
        }
      };
      this.getDeptChildTree();
    },
    //新增/修改
    save:function() {
      this.$refs.saveForm.validate((valid) => {
        console.log("valid"+valid)
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {})
              .then(() => {
                this.saveLoading = true;
                let param = this.saveForm;
                let array = param.department;
                param.department = {id:array[array.length-1]};
                this.$http.put("/shop",param)
                    .then(result => {
                      console.log(result);
                      result = result.data;
                      if (result.success){
                        this.saveLoading = false;
                        this.saveFormVisible=false;
                        this.$message({message:"保存成功!",type:"success"});
                        this.query.currentPage=1;
                        this.getShopList();
                      }else{
                        this.$message({message:result.message,type:"error"});
                      }
                    })
                    .catch(result =>{
                      this.saveLoading = false;
                      this.$message({message:"网络异常,请检查网络!",type:"error"});
                    })
              }).catch(result =>{
            this.$message({message:"已取消保存!",type:"info"});
          })
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      console.log(ids)
      this.$confirm('确认删除多条数据吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.patch("/shop/",ids)
            .then(result => {
              console.log(result);
              result = result.data;
              if (result.success){
                this.$message({message:"批量删除成功!",type:"success"});
                this.query.currentPage=1;
                this.getShopList();
              }else{
                this.$message({message:result.message,type:"error"});
              }
            })
            .catch(result =>{
              this.$message({message:"网络异常,请检查网络!",type:"error"});
            })
      }).catch(() => {
        this.$message({message:"已取消批量删除!",type:"info"});
      });
    }
  },
  mounted() {
    this.getShopList();
  }
}

</script>

<style scoped>

</style>